﻿//==================================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 文件名: DatDepartment.cs
// 作 者：代码自动生成
// 日 期：2012-3-20 14:33:04
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using iSun.DAL;
using OpenExpressApp.ORM;

namespace iSun.Entity
{
    /// <summary>
    /// 部门信息表
    /// </summary>
    [Serializable]
    [Table(Name = "SysDept")]
    [iSun.Model.Table("SysDept", "ID")]
    public class DatDepartment : iSun.Entity.EntityBase
    {

        #region Business Methods 业务方法

        /// <summary>
        /// 部门ID
        /// </summary>
        private static PropertyInfo<Guid> DeptIDProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<Guid>("DeptID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid ID
        {
            get { return GetProperty(DeptIDProperty); }
            set { SetProperty(DeptIDProperty, value); }
        }

        /// <summary>
        /// 部门父节点
        /// </summary>
        private static PropertyInfo<Guid> ParentIDProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<Guid>("ParentID"));
        [Column]
        public Guid ParentID
        {
            get { return GetProperty(ParentIDProperty); }
            set { SetProperty(ParentIDProperty, value); }
        }
        /// <summary>
        /// 部门名称
        /// </summary>
        private static PropertyInfo<string> DeptNameProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("DeptName"));
        [Column]
        public string Name
        {
            get { return GetProperty(DeptNameProperty); }
            set { SetProperty(DeptNameProperty, value); }
        }
        /// <summary>
        /// 长部门名称
        /// </summary>
        private static PropertyInfo<string> LDeptNameProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("LDeptName"));
        [Column]
        public string PathCode
        {
            get { return GetProperty(LDeptNameProperty); }
            set { SetProperty(LDeptNameProperty, value); }
        }


        ///// <summary>
        ///// 层组部门名称
        ///// </summary>
        //private static PropertyInfo<string> TreeDeptNameProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("TreeDeptName"));
        //[Column]
        //public string TreeDeptName
        //{
        //    get { return GetProperty(TreeDeptNameProperty); }
        //    set { SetProperty(TreeDeptNameProperty, value); }
        //}

        /// <summary>
        /// 部门助记编号[编码]
        /// </summary>
        private static PropertyInfo<string> HelpCodeProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("HelpCode"));
        [Column]
        public string HelpCode
        {
            get { return GetProperty(HelpCodeProperty); }
            set { SetProperty(HelpCodeProperty, value); }
        }

        /// <summary>
        /// 主管岗位
        /// </summary>
        private static PropertyInfo<Guid> ManagerProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<Guid>("Manager"));
        [Column]
        public Guid Manager
        {
            get { return GetProperty(ManagerProperty); }
            set { SetProperty(ManagerProperty, value); }
        }
        /// <summary>
        /// 费用归属(1制造费用2研发费用3销售费用4管理费用)
        /// </summary>
        private static PropertyInfo<string> FeesProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("Fees"));
        [Column]
        public string Fees
        {
            get { return GetProperty(FeesProperty); }
            set { SetProperty(FeesProperty, value); }
        }
        /// <summary>
        /// 组织归属[保留]
        /// </summary>
        private static PropertyInfo<int> OrganizeProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<int>("Organize"));
        [Column]
        public int Organize
        {
            get { return GetProperty(OrganizeProperty); }
            set { SetProperty(OrganizeProperty, value); }
        }
        /// <summary>
        /// 状态(OP运行中;CA停用)
        /// </summary>
        private static PropertyInfo<string> StatusProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("Status"));
        [Column]
        public string Status
        {
            get { return GetProperty(StatusProperty); }
            set { SetProperty(StatusProperty, value); }
        }
        /// <summary>
        /// 总公司代号助记
        /// </summary>
        private static PropertyInfo<string> MYNProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("MYN"));
        [Column]
        public string MYN
        {
            get { return GetProperty(MYNProperty); }
            set { SetProperty(MYNProperty, value); }
        }
        /// <summary>
        /// 层级[]
        /// </summary>
        private static PropertyInfo<int> LevelnoProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<int>("Levelno"));
        [Column]
        public int Levelno
        {
            get { return GetProperty(LevelnoProperty); }
            set { SetProperty(LevelnoProperty, value); }
        }
        /// <summary>
        /// 库存助记[保留]
        /// </summary>
        private static PropertyInfo<string> InvFlagProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("InvFlag"));
        [Column]
        public string InvFlag
        {
            get { return GetProperty(InvFlagProperty); }
            set { SetProperty(InvFlagProperty, value); }
        }
        /// <summary>
        /// 职能范围
        /// </summary>
        private static PropertyInfo<string> JFCodeProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("JFCode"));
        [Column]
        public string JFCode
        {
            get { return GetProperty(JFCodeProperty); }
            set { SetProperty(JFCodeProperty, value); }
        }
        /// <summary>
        /// 部门路径,分隔符为"." 例如001.001.002[3位]
        /// </summary>
        private static PropertyInfo<string> DeptPathProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("DeptPath"));
        [Column]
        public string DeptPath
        {
            get { return GetProperty(DeptPathProperty); }
            set { SetProperty(DeptPathProperty, value); }
        }
        /// <summary>
        /// 排序
        /// </summary>
        private static PropertyInfo<int> SortProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<int>("Sort"));
        [Column]
        public int Sort
        {
            get { return GetProperty(SortProperty); }
            set { SetProperty(SortProperty, value); }
        }
        /// <summary>
        /// 生效日期
        /// </summary>
        private static PropertyInfo<DateTime> EffectDateProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<DateTime>("EffectDate"));
        [Column]
        public DateTime EffectDate
        {
            get { return GetProperty(EffectDateProperty); }
            set { SetProperty(EffectDateProperty, value); }
        }
        /// <summary>
        /// 失效日期
        /// </summary>
        private static PropertyInfo<DateTime> ExpireDateProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<DateTime>("ExpireDate"));
        [Column]
        public DateTime ExpireDate
        {
            get { return GetProperty(ExpireDateProperty); }
            set { SetProperty(ExpireDateProperty, value); }
        }
        /// <summary>
        /// 说明
        /// </summary>
        private static PropertyInfo<string> DescriptionProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("Description"));
        [Column]
        public string Description
        {
            get { return GetProperty(DescriptionProperty); }
            set { SetProperty(DescriptionProperty, value); }
        }
        /// <summary>
        /// 建档日
        /// </summary>
        private static PropertyInfo<DateTime> AddTimeProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<DateTime>("AddTime"));
        [Column]
        public DateTime AddTime
        {
            get { return GetProperty(AddTimeProperty); }
            set { SetProperty(AddTimeProperty, value); }
        }
        /// <summary>
        /// 创建人
        /// </summary>
        private static PropertyInfo<Guid> AddUserProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<Guid>("AddUser"));
        [Column]
        public Guid AddUser
        {
            get { return GetProperty(AddUserProperty); }
            set { SetProperty(AddUserProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> HelpTreeCodeProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("HelpTreeCode"));
        [Column]
        public string HelpTreeCode
        {
            get { return GetProperty(HelpTreeCodeProperty); }
            set { SetProperty(HelpTreeCodeProperty, value); }
        }
        /// <summary>
        /// 是否为外部部门
        /// </summary>
        private static PropertyInfo<int> IsOutDepartmentProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<int>("IsOutDepartment"));
        [Column]
        public int IsOutDepartment
        {
            get { return GetProperty(IsOutDepartmentProperty); }
            set { SetProperty(IsOutDepartmentProperty, value); }
        }
        /// <summary>
        /// 是否是虚拟部门
        /// </summary>
        private static PropertyInfo<int> IsVirtualDepartmentProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<int>("IsVirtualDepartment"));
        [Column]
        public int IsVirtualDepartment
        {
            get { return GetProperty(IsVirtualDepartmentProperty); }
            set { SetProperty(IsVirtualDepartmentProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<int> deptTypeProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<int>("deptType"));
        [Column]
        public int deptType
        {
            get { return GetProperty(deptTypeProperty); }
            set { SetProperty(deptTypeProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> EmailProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("Email"));
        [Column]
        public string Email
        {
            get { return GetProperty(EmailProperty); }
            set { SetProperty(EmailProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> URLProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("URL"));
        [Column]
        public string URL
        {
            get { return GetProperty(URLProperty); }
            set { SetProperty(URLProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> AddressProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("Address"));
        [Column]
        public string Address
        {
            get { return GetProperty(AddressProperty); }
            set { SetProperty(AddressProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ArtificialPersonProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("ArtificialPerson"));
        [Column]
        public string ArtificialPerson
        {
            get { return GetProperty(ArtificialPersonProperty); }
            set { SetProperty(ArtificialPersonProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> CompanyIDProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<Guid>("CompanyID"));
        [Column]
        public Guid CompanyID
        {
            get { return GetProperty(CompanyIDProperty); }
            set { SetProperty(CompanyIDProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> DeptFunctionProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("DeptFunction"));
        [Column]
        public string DeptFunction
        {
            get { return GetProperty(DeptFunctionProperty); }
            set { SetProperty(DeptFunctionProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> PhoneProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("Phone"));
        [Column]
        public string Phone
        {
            get { return GetProperty(PhoneProperty); }
            set { SetProperty(PhoneProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> PostCodeProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("PostCode"));
        [Column]
        public string PostCode
        {
            get { return GetProperty(PostCodeProperty); }
            set { SetProperty(PostCodeProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> FaxProperty = RegisterProperty(typeof(DatDepartment), new PropertyInfo<string>("Fax"));
        [Column]
        public string Fax
        {
            get { return GetProperty(FaxProperty); }
            set { SetProperty(FaxProperty, value); }
        }
        public string iSun_ManagerName
        {
            get;
            set;
        }
        #endregion

        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "DeptName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("DeptName", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "LDeptName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("LDeptName", 150));
            ValidationRules.AddRule(CommonRules.StringRequired, "TreeDeptName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("TreeDeptName", 150));
            ValidationRules.AddRule(CommonRules.StringRequired, "HelpCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("HelpCode", 40));
            ValidationRules.AddRule(CommonRules.StringRequired, "Fees");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Fees", 1));
            ValidationRules.AddRule(CommonRules.StringRequired, "Status");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Status", 2));
            ValidationRules.AddRule(CommonRules.StringRequired, "MYN");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("MYN", 1));
            ValidationRules.AddRule(CommonRules.StringRequired, "InvFlag");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("InvFlag", 1));
            ValidationRules.AddRule(CommonRules.StringRequired, "JFCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("JFCode", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "DeptPath");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("DeptPath", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "Description");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Description", 150));
            ValidationRules.AddRule(CommonRules.StringRequired, "HelpTreeCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("HelpTreeCode", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "Email");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Email", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "URL");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("URL", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "Address");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Address", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "ArtificialPerson");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ArtificialPerson", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "DeptFunction");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("DeptFunction", 500));
            ValidationRules.AddRule(CommonRules.StringRequired, "Phone");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Phone", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "PostCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("PostCode", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "Fax");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Fax", 30));
            //base.AddBusinessRules();
        }
        #endregion

        #region Factory Methods 工厂方法

        public DatDepartment() { }

        public static DatDepartment New()
        {
            return DataPortal.Create<DatDepartment>();
        }
        public static DatDepartment Get(Guid DeptID)
        {
            return DataPortal.Fetch<DatDepartment>(new SingleLazyCriteria<DatDepartment, Guid>(DeptID, false));
        }
        public static void Delete(Guid DeptID)
        {
            DataPortal.Delete(DeptID);
        }

        //public override DatDepartment Save()
        //{
        //return base.Save();
        //}
        #endregion

        #region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid DeptID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("DeptID").Equal(DeptID);
                var data = db.Select<DatDepartment>(q)[0];
                LoadProperty(DeptIDProperty, data.ID);
                LoadProperty(ParentIDProperty, data.ParentID);
                LoadProperty(DeptNameProperty, data.Name);
                LoadProperty(LDeptNameProperty, data.PathCode);
                //LoadProperty(TreeDeptNameProperty, data.TreeDeptName);
                LoadProperty(HelpCodeProperty, data.HelpCode);
                LoadProperty(ManagerProperty, data.Manager);
                LoadProperty(FeesProperty, data.Fees);
                LoadProperty(OrganizeProperty, data.Organize);
                LoadProperty(StatusProperty, data.Status);
                LoadProperty(MYNProperty, data.MYN);
                LoadProperty(LevelnoProperty, data.Levelno);
                LoadProperty(InvFlagProperty, data.InvFlag);
                LoadProperty(JFCodeProperty, data.JFCode);
                LoadProperty(DeptPathProperty, data.DeptPath);
                LoadProperty(SortProperty, data.Sort);
                LoadProperty(EffectDateProperty, data.EffectDate);
                LoadProperty(ExpireDateProperty, data.ExpireDate);
                LoadProperty(DescriptionProperty, data.Description);
                LoadProperty(AddTimeProperty, data.AddTime);
                LoadProperty(AddUserProperty, data.AddUser);
                LoadProperty(HelpTreeCodeProperty, data.HelpTreeCode);
                LoadProperty(IsOutDepartmentProperty, data.IsOutDepartment);
                LoadProperty(IsVirtualDepartmentProperty, data.IsVirtualDepartment);
                LoadProperty(deptTypeProperty, data.deptType);
                LoadProperty(EmailProperty, data.Email);
                LoadProperty(URLProperty, data.URL);
                LoadProperty(AddressProperty, data.Address);
                LoadProperty(ArtificialPersonProperty, data.ArtificialPerson);
                LoadProperty(CompanyIDProperty, data.CompanyID);
                LoadProperty(DeptFunctionProperty, data.DeptFunction);
                LoadProperty(PhoneProperty, data.Phone);
                LoadProperty(PostCodeProperty, data.PostCode);
                LoadProperty(FaxProperty, data.Fax);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(DeptIDProperty);
        }
        #endregion

    }
}

